1 // Fig. 15.9: stack.h 2 // Stack class template definition 3 // Derived from class List 4 #ifndef STACK_H 5 #define STACK_H 6 7 #include "list.h" 8 9 template< class STACKTYPE > 10 class Stack : private List< STACKTYPE > { 11 public: 12 void push( const STACKTYPE &d ) { insertAtFront( d ); } 13 bool pop( STACKTYPE &d ) { return removeFromFront( d ); } 14 bool isStackEmpty() const { return isEmpty(); } 15 void printStack() const { print(); } 16 }; 17 18 #endif 19 20 21 // Fig. 15.9: fig15_09.cpp 22 // Driver to test the template Stack class 23 #include 24 #include "stack.h" 25 26 int main() 27 { 28 Stack< int > intStack; 29 int popInteger; 30 cout << "processing an integer Stack" << endl; 31 32 for ( int i = 0; i < 4; i++ ) { 33 intStack.push( i ); 34 intStack.printStack(); 35 } 36 37 while ( !intStack.isStackEmpty() ) { 38 intStack.pop( popInteger ); 39 cout << popInteger << " popped from stack" << endl; 40 intStack.printStack(); 41 } 42 43 Stack< double > doubleStack; 44 double val = 1.1, popdouble; 45 cout << "processing a double Stack" << endl; 46 47 for ( i = 0; i < 4; i++ ) { 48 doubleStack.push( val ); 49 doubleStack.printStack(); 50 val += 1.1; 51 } 52 53 while ( !doubleStack.isStackEmpty() ) { 54 doubleStack.pop( popdouble ); 55 cout << popdouble << " popped from stack" << endl; 56 doubleStack.printStack(); 57 } 58 return 0; 59 }